from base import Yaml, Path, build_request, Json


class MemberApi:
    _member_yaml = Yaml.get_config_as_dict(Path.get_actual_path_by_current_file(__file__, 'member.yaml'))['MemberApi']
    _base_url = None

    def __init__(self, url):
        self._base_url = url

    def login(self, sign, account, password):
        """
        前台买家登录
        :param sign: 签名
        :param account: 用户名
        :param password: 密码
        :return: ret
                 data
                     result
                          is_login
                          uid
                          token
                          status
                     code
                     msg
                 msg
        """

        url = '%s?service=%s' % (self._base_url, self._member_yaml['MEMBRE_URL'])
        login_data = {'sign': sign, 'account': account, 'password': password}
        req = build_request(url=url, method='p', params=login_data)
        return self._get_body_resp(req.json_dict)

    def _get_body_resp(self, json_dict):
        """
        解析json_dict
        :param json_dict:
        :return:
               result
                      is_login
                      uid
                      token
                      status
                code
                msg
        """

        body_resp = {}
        body_resp[self._member_yaml['BODY_RESP_IS_LOGIN']] = Json.parse_json_dict_value(json_dict,
                                                                                        data_key=self._member_yaml[
                                                                                            'IS_LOGIN_DATAKEY'],
                                                                                        index=self._member_yaml[
                                                                                            'IS_LOGIN_INDEX'],
                                                                                        sub_key=self._member_yaml[
                                                                                            'IS_LOGIN_SUBKEY'])
        body_resp[self._member_yaml['BODY_RESP_UID']] = Json.parse_json_dict_value(json_dict,
                                                                                   data_key=self._member_yaml[
                                                                                       'UID_DATAKEY'],
                                                                                   index=self._member_yaml[
                                                                                       'UID_INDEX'],
                                                                                   sub_key=self._member_yaml[
                                                                                       'UID_SUBKEY'])
        body_resp[self._member_yaml['BODY_RESP_TOKEN']] = Json.parse_json_dict_value(json_dict,
                                                                                     data_key=self._member_yaml[
                                                                                         'TOKEN_DATAKEY'],
                                                                                     index=self._member_yaml[
                                                                                         'TOKEN_INDEX'],
                                                                                     sub_key=self._member_yaml[
                                                                                         'TOKEN_SUBKEY'])
        body_resp[self._member_yaml['BODY_RESP_STATUS']] = Json.parse_json_dict_value(json_dict,
                                                                                      data_key=self._member_yaml[
                                                                                          'STATUS_DATAKEY'],
                                                                                      index=self._member_yaml[
                                                                                          'STATUS_INDEX'],
                                                                                      sub_key=self._member_yaml[
                                                                                          'STATUS_SUBKEY'])
        body_resp[self._member_yaml['BODY_RESP_CODE']] = Json.parse_json_dict_value(json_dict,
                                                                                    data_key=self._member_yaml[
                                                                                        'CODE_DATAKEY'])
        body_resp[self._member_yaml['BODY_RESP_MSG']] = Json.parse_json_dict_value(json_dict,
                                                                                   data_key=self._member_yaml[
                                                                                       'MSG_DATAKEY'])

        return body_resp

